/*
 * @author: lhy
 */
//配置路由，

import {createRouter, createWebHistory} from 'vue-router'
import Home from "../views/Home.vue";
import NotFound from "../views/NotFound.vue";
import Login from "../views/Login.vue";
import {useTokenStore} from "../store/tokenStore.ts";


const router = createRouter({
    // history: createWebHashHistory(), // hash  #/film #/center
    history: createWebHistory(), // 
    // history /film /center
    routes :[
        {
            path:"/login",
            name:"login",
            component:Login
        },
        {
            path:"/home",
            name:"home",
            component:Home
        },
        {
            path:"/user/account",
            name:"userAccount",
            component:()=>import("../views/UserAccount.vue")
        },
        {
            path:"/user/group",
            name:"userGroup",
            component:()=>import("../views/UserGroup.vue")
        },
        {
            path:"/user/role",
            name:"userRole",
            component:()=>import("../views/UserRole.vue")
        },
        {
            path:"/user/permission",
            name:"userPermission",
            component:()=>import("../views/UserPermission.vue")
        },
        {
            path:"/menu",
            name:"menu",
            component:()=>import("../views/Menu.vue")
        },
        {
            path:"/resource/list",
            name:"resourceList",
            component:()=>import("../views/ResourceList.vue")
        },
        {
            path:"/resource/group",
            name:"resourceGroup",
            component:()=>import("../views/ResourceGroup.vue")
        },
        {
            path:"/setting",
            name:"setting",
            component:()=>import("../views/Setting.vue")
        },
        {
            path:"/",
            redirect:"/home"
        },
        {   path: '/:pathMatch(.*)*',
            component: NotFound
        }
    ]

})


// 所有页面都拦截过滤
router.beforeEach(async (to, _, next) => {
    let isAuthenticated = useTokenStore().checkAuthenticated()
    if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
    else {
        next()
    }
})

export default router
